package com.cmy.servlet;

import com.cmy.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class SessionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //得到session
        HttpSession session = req.getSession();

        //session中存入信息 ,字符/对象 session存的信息比cookie多且复杂
        session.setAttribute("name",new Person("void陈",22));

        //获取session的id,唯一标识符
        String sessionId = session.getId();

        //判读session是否为新创建的
        if (session.isNew()) {
            resp.getWriter().print("session创建成功,id为:"+sessionId);
        }else {
            resp.getWriter().print("session已经在服务器中存在了,id为:"+sessionId);
        }
        //session创建时做了什么?
//        Cookie cookie = new Cookie("JSESSIONID",sessionId);
//        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

    